/**
 * @Author: Bobby
 * @Date: 2021-12-30 10:01:42
 * @LastEditTime: 2022-01-08 18:57:54
 * @LastEditors: Bobby
 * @Description:
 * @FilePath: \wms-v2\src\utils\utils.js
 * @CIMC-V2
 */
// 依赖基座
import store from '@commonProject/store'
import { cloneDeep } from 'lodash'
// import { openDashboardMenu } from '@project/config'

//获取config内容 Get File Config Data
export function GFCD(configName, name) {
  let data = store.getters['fileConfig/data']
  if (configName && name) {
    return data[configName][name]
  }
  if (configName) {
    return data[configName]
  }
}
function recursionTree(list, pid) {
  // if()
  return list.reduce((res, current) => {
    if (current['MENU_SCODE'] === pid) {
      current.CHILDREN = recursionTree(list, current['MENU_CODE'])
      return res.concat(current)
    }
    return res
  }, [])
}
//menu转Routes
export function menuToRoutes(list, code = '', flag) {
  let arr = flag ? list : recursionTree(list, '')
  console.log(cloneDeep(arr))
  let menu = menuToRoutesTree(arr, 0, code)
  return menu
}

//后台返回菜单数据 转 路由tree
function menuToRoutesTree(list, index, code) {
  list.forEach((v) => {
    if (v.SORT == '') {
      v.SORT = 1000
    }
  })
  list = list.sort((a, b) => {
    return a.SORT - b.SORT
  })

  //层级
  let menu = []
  list.forEach((v) => {
    // console.log(v, i, openDashboardMenu, code, index)
    // console.log(index == 0 ? `/${v.MENU_CODE}` : `${v.MENU_CODE}`)
    let _menu = {
      // path: index == 0 ? (v.MENU_URL == '' && i == 0 ? `/${openDashboardMenu ? code : ''}` : `/${v.MENU_CODE}`) : `${v.MENU_CODE}`,
      // name: index == 0 && i == 0 ? 'Root' : `${v.MENU_CODE}`,
      //如果某个系统控制台
      path: index == 0 ? `/${v.MENU_CODE}` : `${v.MENU_CODE}`,
      name: `${v.MENU_CODE}`,
      component: index == 0 ? 'Layout' : v.MENU_URL ? v.MENU_URL : 'views/index',
      props: { menuId: v.MENU_CODE, SUB_SYSTEM: v.SUB_SYSTEM },
      type: v.TYPE,
      meta: {
        menuId: v.MENU_CODE, //后面用-routes
        title: v.MENU_NAME,
        icon: v.MENU_ICON || 'home-2-line',
        // breadcrumbHidden: true,
        // hidden: v.HIDDEN || false,
        hidden: v.HIDDEN == '1' ? false : true,
        pageType: v.TYPE,
        //自定义控制台
        breadcrumbHidden: v.MENU_CODE === '/' ? true : false,
        levelHidden: v.MENU_CODE === '/' ? true : false,
      },
    }

    if (v.CHILDREN && v.CHILDREN.length > 0) {
      _menu.children = menuToRoutesTree(v.CHILDREN, 1, code)
    }
    menu.push(_menu)
  })
  return menu
}
